package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Exam;
import cn.wolfcode.crm.query.ExamQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IExamService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("exam")
public class ExamController {

    @Autowired
    IExamService examService;

    @RequestMapping("")
    @RequiresPermissions(value = {"考试管理首页","exam:index"} ,logical = Logical.OR)
    public String index(){

        return "exam/list";
    }

    @RequestMapping("list")
    @RequiresPermissions(value = {"考试管理列表","exam:list"} ,logical = Logical.OR)
    @ResponseBody
    public PageResult list(ExamQueryObject qo){
        PageResult result = examService.query(qo);
        return result;
    }
    @RequestMapping("listData")
    @ResponseBody
    public Object listData(QueryObject qo){

        return examService.list();
    }

    @RequestMapping("save")
    @RequiresPermissions(value = {"考试管理保存","exam:save"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult save(Exam entity){
        JSONResult result = new JSONResult();
        try{
            examService.save(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }

    @RequestMapping("update")
    @RequiresPermissions(value = {"考试管理更新","exam:update"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult update(Exam entity){
        JSONResult result = new JSONResult();
        try{
            examService.update(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }


    @RequestMapping("delete")
    @RequiresPermissions(value = {"考试管理删除","exam:delete"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id){
        JSONResult result = new JSONResult();
        try{
            examService.delete(id);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }
    @RequestMapping("audit")
    @ResponseBody
    public JSONResult audit(Long id){
        JSONResult result = new JSONResult();
        try{
            examService.audit(id);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }
}
